<?php
/**
*   Write (append) log messages to text file
*
*
**/
class Yz_Log_Writer_Text 
    extends Yz_Class 
    implements Yz_Log_Writer_Interface
{

    /**
    *     @var string file : file name
    */
    protected $file;


    /**
    *   initialization
    *   @param string fileName : path to log file
    */
    public function __construct( $fileName ) 
    {
        if ( 
            'php://stderr' === $fileName
            || 'php://stdout' === $fileName
            || (is_file($fileName) && is_writable($fileName))
        ) {
            $this->file = $fileName;
        }
    }

    /**
    *
    */
    public function write( $logId, $msg ) 
    {

        // if log not initialized or no logging required, do nothing 
        if ( null === $this->file ) {
            return 0;
        }

        $msg = sprintf( "[%s]+%.3fs %s\n",
            $logId,
            Yz_Date::getElapsedTime(),
            $msg
        );
        file_put_contents( $this->file, $msg, FILE_APPEND );

        return 0;
    }

} // class

